
%% IMPORT and Organize DATA

names = fullnames_data(choose_variab)
bettername = betternames_data(choose_variab) 
region = region_data(choose_variab)
names_exog = fullnames_data(choose_exog)
Y = data_all(:,choose_variab);
Y_exog=data_all(:,choose_exog);

% chose names for IRFs
names_IRFs = fullnames_data(choose_variab);
transformIRF_used = transformIRFs_data(choose_variab);
labelIRF_used = labelIRFs_data(choose_variab);
Numbers = 1:length(day);
Numbers=Numbers';

% reduce to period for which all measures are available
cond = isnan(Y);
cond2 = sum(cond,2) == 0;
Y = Y(cond2,:);
Y_exog = Y_exog(cond2,:);
year_available     = year(cond2);
month_available    = month(cond2);
day_available    = day(cond2);
Numbers_available = Numbers(cond2);
% plot(Y(:,2));

data_all_available = data_all(cond2,:);
% keep only after a certain period, in case data are available for all variables added for before that period

% full sample
from = 1 % + 2610 + 00;
until = 30000; % end 2015

cond = and(Numbers_available >= from,Numbers_available <= until);
sum(cond)
Y = Y(cond,:);
Y_exog = Y_exog(cond,:);

year_available = year_available(cond);
month_available = month_available(cond);
day_available    = day_available(cond);
Numbers_available = Numbers_available(cond);

disp(['-->> Data available for :    ', num2str(year_available(1)) 'M' num2str(month_available(1)) 'D' num2str(day_available(1)) ...
                            ' until ', num2str(year_available(end)) 'M' num2str(month_available(end)) 'D' num2str(day_available(end))] );
% ensure Y enters horizontally
Y = Y';
Y_exog = Y_exog';
k = size(Y,1)
T = size(Y,2)
assert(  T > k)
assert( length(year_available) == T)      

year_available_help=zeros(T,1);
for i=1:length(year_available)
year_available_help(i,:) = year_available(i,:) + 0.001*i;
end

%% Estimate RVAR
maxlags = 20;
 
constantD   = 1;
trendD      = 0;
trendBreaks = [];
seasonD     = 0;
moreBreaks  = [];


[Ahat, Resid, Sigmahat, Ahat_determ, X_determ, T_used, df_lost] = ...
         zFC_VAR_Estimation_exog(Y,p,constantD,trendD,trendBreaks,seasonD,moreBreaks,exogD,Y_exog);        
        
year_available_used     = year_available(p+1:end);
assert(length(year_available_used) == size(Resid,2))
month_available_used    = month_available(p+1:end);
day_available_used    = day_available(p+1:end);
Numbers_available_used = Numbers_available(p+1:end);


%% Select period and data available for all variables

Instrument = data_all(:,choose_shock);
cond_i = isnan(Instrument);
cond2_i = sum(cond_i,2) == 0;
Instrument = Instrument(cond2_i,:);
year_available_ins     = year(cond2_i);
month_available_ins    = month(cond2_i);
day_available_ins = day(cond2_i);
Numbers_available_ins = Numbers(cond2_i);

% select subperiod common to shocks used and variables used
from2_year         = max(year_available_used(1),year_available_ins(1));
from2_month          = max(month_available_used(1),month_available_ins(1));
from2_day          = max(day_available_used(1),day_available_ins(1));
from2          = max(Numbers_available_used(1),Numbers_available_ins(1));
% % % % %from2          = timeaxis_used(59);
until2_year         = min(year_available_used(end),year_available_ins(end));
until2_month         = min(month_available_used(end),month_available_ins(end));
until2_day         = min(day_available_used(end),day_available_ins(end));
until2         = min(Numbers_available_used(end),Numbers_available_ins(end));
Numers_available_used2 = [from2:until2];
                      
% Take only the data in this subperiod
cond1            = Numbers_available_ins >= from2 & Numbers_available_ins <= until2;
Instruments_used = Instrument(cond1,:);
n_instrum=size(Instrument);
n_instrum=n_instrum(1,2);
 
% % plot raw interventions and inverventions actually used in the identification

tick=Numbers_available_used;

figure(1)
    subplot(2,1,1)
    plot(Instruments_used(:,1),'k', 'Linewidth', 2), hold on
    title('All interventions','FontSize',16)
    ylabel('USD bn','FontSize',16)    
    axis tight
    set(gca,'XTick',[1,length(Instruments_used(:,1))])
    set(gca,'XTickLabel',[year_available_ins(1),year_available_ins(end)])
    set(gca,'box','off','FontSize',12)

    subplot(2,1,2)
    plot(Instruments_used(:,2),'k', 'Linewidth', 2), hold on
    title('Start of sequence','FontSize',16)
    ylabel('Purchas/Sell USD','FontSize',16)    
    axis tight
    set(gca,'XTick',[1,length(Instruments_used(:,1))])
    set(gca,'XTickLabel',[year_available_ins(1),year_available_ins(end)])
    set(gca,'box','off','FontSize',12)

